package com.mango.ch08;

import java.io.IOException;
import java.util.LinkedList;
import java.util.List;
import java.util.Set;
import java.util.TreeSet;
import org.apache.hadoop.io.Text;
import org.apache.hadoop.mapreduce.Reducer;
import com.mango.Tools.StringUtils;

public class ComFriendsReducer extends Reducer<Text, Text, Text, Text> {

	@Override
	protected void reduce(Text key, Iterable<Text> values, Reducer<Text, Text, Text, Text>.Context context)
			throws IOException, InterruptedException {
		List<TreeSet<Integer>> result = new LinkedList<>();
		System.out.println("into reducer");
		for (Text text : values) {
			String val = text.toString();
			// 去除首尾的"[" "]"
			val = val.substring(1, val.length() - 1);
			System.out.println("规约后的key对应的值：" + val);
			TreeSet<Integer> list = new TreeSet<>();
			list.addAll(StringUtils.strToSet(val));
			result.add(list);
		}

		Set<Integer> comFri = CommonFriends.intersection(result.get(0), result.get(1));
		context.write(new Text(key.toString()), new Text(comFri.toString()));

	}

}
